package org.gdstash.ui.character;

import java.util.Collection;
import java.util.LinkedList;
import org.gdstash.ui.stash.GDUIContainer;
import java.util.List;
import org.gdstash.item.GDAbstractContainer;
import java.util.Iterator;
import org.gdstash.character.GDCharInventorySack;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.UIManager;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.Container;
import javax.swing.GroupLayout;
import java.awt.image.BufferedImage;
import org.gdstash.ui.GDUITransfer;
import javax.swing.JPanel;
import org.gdstash.item.GDItem;
import org.gdstash.character.GDChar;
import org.gdstash.ui.stash.GDContainerPane;
import javax.swing.JTabbedPane;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.stash.GDUIInventory;
import org.gdstash.ui.stash.GDContainerMapPane;

public class GDCharInventoryBagPane extends GDContainerMapPane implements GDUIInventory
{
    private GDStashFrame frame;
    private JTabbedPane tabPages;
    private GDContainerPane[] pnlPages;
    private int filledPages;
    private GDChar gdc;
    private GDItem selItem;
    private GDContainerPane selPage;
    private JPanel pnlMain;
    private GDUIInventory uiInventory;
    private GDUITransfer uiTransfer;
    private boolean blockEvents;
    
    public GDCharInventoryBagPane(final GDStashFrame frame, final GDUIInventory uiInventory, final GDUITransfer uiTransfer) {
        super(uiInventory, uiTransfer, null);
        this.frame = frame;
        this.uiInventory = uiInventory;
        this.uiTransfer = uiTransfer;
        this.pnlPages = new GDContainerPane[5];
        this.filledPages = 0;
        this.selItem = null;
        this.selPage = null;
        this.blockEvents = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        this.pnlMain = this.buildMainPanel();
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlMain));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlMain));
        layout.setVerticalGroup(vGroup);
    }
    
    @Override
    public void adjustUI() {
        final Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntTabbed = UIManager.getDefaults().getFont("TabbedPane.font");
        if (fntTabbed == null) {
            fntTabbed = fntLabel;
        }
        fntTabbed = fntTabbed.deriveFont((float)GDStashFrame.iniConfig.sectUI.fontSize);
        if (this.tabPages == null) {
            (this.tabPages = new JTabbedPane()).setTabPlacement(2);
            this.tabPages.add("I", this.pnlPages[0]);
            this.tabPages.add("II", this.pnlPages[1]);
            this.tabPages.add("III", this.pnlPages[2]);
            this.tabPages.add("IV", this.pnlPages[3]);
            this.tabPages.add("V", this.pnlPages[4]);
        }
        this.tabPages.setFont(fntTabbed);
        this.tabPages.setMaximumSize(new Dimension(this.pnlPages[0].getPreferredWidth() + 80, this.pnlPages[0].getPreferredHeight()));
        this.tabPages.setPreferredSize(new Dimension(this.pnlPages[0].getPreferredWidth() + 80, this.pnlPages[0].getPreferredHeight()));
    }
    
    private JPanel buildMainPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel panel = new JPanel();
        this.layoutStash();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.tabPages));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.tabPages));
        layout.setVerticalGroup(vGroup);
        return panel;
    }
    
    public GDContainerPane getCurrentPage() {
        final int index = this.tabPages.getSelectedIndex();
        return this.pnlPages[index];
    }
    
    public void layoutStash() {
        int i = 0;
        if (this.gdc != null) {
            for (final GDAbstractContainer page : this.gdc.getBags()) {
                ++i;
            }
        }
        while (i < this.pnlPages.length) {
            ++i;
        }
    }
    
    public void deleteSelectedItem(final int action) {
        if (this.selItem == null) {
            return;
        }
        final boolean success = this.selPage.deleteItem(this.selItem, action, true);
        if (success) {
            this.setSelectedItem(null);
        }
    }
    
    public void moveSelectedItem(final int action, final int x, final int y) {
        if (this.selItem == null) {
            return;
        }
        final GDContainerPane page = this.getCurrentPage();
        final GDItem clone = this.selItem.clone();
        boolean success = page.addItem(clone, action, x, y);
        if (success) {
            success = this.selPage.deleteItem(this.selItem, action, true);
            if (success) {
                this.selItem = clone;
                this.selPage = page;
            }
        }
    }
    
    @Override
    public void setBGImage(final BufferedImage bgImage) {
    }
    
    @Override
    public void setChar(final GDChar gdc) {
        this.gdc = gdc;
        this.clearContainers();
        if (gdc != null) {
            final List<GDCharInventorySack> list = gdc.getBags();
            for (GDCharInventorySack gdCharInventorySack : list) {}
        }
    }
    
    @Override
    public void clearContainers() {
        this.gdc = null;
        for (int i = 0; i < this.pnlPages.length; ++i) {
            this.pnlPages[i].refresh();
        }
        this.tabPages.removeAll();
        this.filledPages = 0;
        this.selPage = null;
        this.layoutContainers();
    }
    
    @Override
    public void addContainer(final GDUIContainer container) {
        final GDContainerPane pane = (GDContainerPane)container;
        this.pnlPages[this.filledPages] = pane;
        String s = "";
        if (this.filledPages == 0) {
            s = "I";
        }
        if (this.filledPages == 1) {
            s = "II";
        }
        if (this.filledPages == 2) {
            s = "III";
        }
        if (this.filledPages == 3) {
            s = "IV";
        }
        if (this.filledPages == 4) {
            s = "V";
        }
        this.tabPages.add(s, this.pnlPages[this.filledPages]);
        ++this.filledPages;
        this.selPage = this.getCurrentPage();
        this.layoutContainers();
    }
    
    @Override
    public void layoutContainers() {
        this.repaint();
    }
    
    @Override
    public boolean deleteItem(final GDItem item, final int action, final boolean update) {
        if (item == null) {
            return true;
        }
        if (this.pnlPages == null) {
            return false;
        }
        boolean deleted = false;
        for (int i = 0; i < this.pnlPages.length; ++i) {
            final GDContainerPane page = this.pnlPages[i];
            if (page != null && page.deleteItem(item, action, update)) {
                deleted = true;
                break;
            }
        }
        if (update) {
            this.layoutContainers();
        }
        return deleted;
    }
    
    @Override
    public boolean hasChanged() {
        if (this.pnlPages == null) {
            return false;
        }
        boolean hasChanged = false;
        for (int i = 0; i < this.pnlPages.length; ++i) {
            final GDContainerPane page = this.pnlPages[i];
            if (page.hasChanged()) {
                hasChanged = true;
                break;
            }
        }
        return hasChanged;
    }
    
    @Override
    public List<GDItem> getItemList(final int action) {
        final List<GDItem> listAll = new LinkedList<GDItem>();
        final GDContainerPane page = this.getCurrentPage();
        if (page == null) {
            return listAll;
        }
        final List<GDItem> list = page.getItemList(action);
        if (list != null) {
            listAll.addAll(list);
        }
        return listAll;
    }
    
    @Override
    public GDItem getSelectedItem() {
        return this.selItem;
    }
    
    @Override
    public void setSelectedItem(final GDItem item) {
        this.selItem = item;
        if (item == null) {
            this.selPage = null;
        }
        else {
            (this.selPage = this.getCurrentPage()).layoutPage();
        }
        this.uiTransfer.setSelectedItem(item, 1);
    }
    
    @Override
    public void refresh() {
        if (this.pnlPages != null) {
            for (int i = 0; i < this.pnlPages.length; ++i) {
                this.pnlPages[i].refresh();
            }
        }
    }
    
    @Override
    public boolean addItem(final GDItem item, final int action, final int x, final int y) {
        if (item == null) {
            return false;
        }
        final GDContainerPane page = this.getCurrentPage();
        return page.addItem(item, action, x, y);
    }
}
